#include "stdafx.h"
#include "WidgetWin.h"
#include "res.h"
#include "Edit.h"
#include "RootView.h"
#include "autotest/Manage.h"
#include "TreeView.h"
#include "autotest/Suit.h"
#include "autotest/Case.h"
#include "Utility.h"

void WidgetWin::thread_fun(std::string str)
{
	// Sleep(3000);

	Edit* edit = reinterpret_cast<Edit *>(WidgetWin::getInstance().getRootView()->getViewById(ID_EDIT_LOG));
	Manage& manager = Manage::getInstance();
	if (!manager.prepare())
	{
		if (edit)
		{
			edit->appendText(std::wstring(L"Serial or Config file error\r"));
		}
		// return;
	}

	std::vector<Suit*> vect_suit = manager.getInstance().getSuits();
	TreeView* tree_view = reinterpret_cast<TreeView *>(WidgetWin::getInstance().getRootView()->getViewById(ID_TREEVIEW_TESTCASE));

	for (std::vector<Suit*>::iterator iter = vect_suit.begin(); iter != vect_suit.end(); ++iter)
	{
		if ((*iter))
		{
			std::string suit_name = (*iter)->getName();
			std::shared_ptr<TreeItem> item = tree_view->AddString(StringToWstring(suit_name));
			std::vector<Case*> vect_case = (*iter)->getCases();
			for (std::vector<Case*>::iterator case_iter = vect_case.begin(); case_iter != vect_case.end(); ++case_iter)
			{
				if (*case_iter)
				{
					std::string case_name = (*case_iter)->getName();
					tree_view->AddString(StringToWstring(case_name), item);
				}
			}
		}
	}

	tree_view->updateView();

	while (!Manage::getInstance().isNeedRun())
	{
		Sleep(500);
	}

	while (1)
	{
		Manage::getInstance().run(NULL);


		Sleep(1000);
	}

/*
	for (unsigned int i = 0; i < 10000000; i++)
	{
		wchar_t szBuff[8] = { 0 };
		wsprintf(szBuff, L"%u", i);
		// need lock
		Edit* edit = reinterpret_cast<Edit *>(WidgetWin::getInstance().getRootView()->getViewById(ID_EDIT_LOG));
		if (edit)
		{
			edit->appendText(std::wstring(L" test !!!") + szBuff);
			Sleep(10);
		}
	}
*/
	/*
		Task task;
		task.setTick();
		MessageLoop::getInstance().addTask();
		*/
}